package com.example.ssm.controller;

import com.example.ssm.entity.Book;
import com.example.ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/books")
public class BookController {
    @Autowired
    BookService bookService;
    @PostMapping("/")
    @PreAuthorize("hasRole('MODERATOR')")
    public Book addBook(@RequestBody Book book){
        bookService.addBook(book);
        return book;
    }
    @PutMapping("/")
    @PreAuthorize("hasRole('MODERATOR')")
    public String editBook(@RequestBody Book book){
        return  bookService.editBook(book);
    }
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('MODERATOR')")
    public String deleteById(@PathVariable Long id){
        return bookService.deleteById(id);
    }
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('MODERATOR')")
    public Book findById(@PathVariable Long id){
        return bookService.findById(id);
    }
    @GetMapping("/")
    @PreAuthorize("hasRole('MODERATOR')")
    public List<Book> findAll(){
        return bookService.findAll();
    }

    @GetMapping("/minId")
    @PreAuthorize("hasRole('MODERATOR')")
    public Book getMinIdBook(){
        return bookService.getMinIdBook();
    }


}
